import * as THREE from "three";
import {deepMerge} from "@/views/map/utils/utils.js";
// 引入dat.gui.js的一个类GUI
import { GUI } from 'dat.gui';

// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

/**
 * @description 创建场景
 * @returns {Scene}
 * @constructor
 */
export  function UseCreateScene(options){
    const scene = new THREE.Scene();
    scene.background = new THREE.Color(0x000000);
    // scene.fog = new THREE.Fog(0x000000, 0, 500)

    let light = UseCreateLight('spotLight');
    let light2 = UseCreateLight('ambientLight');
    scene.add(light);
    scene.add(light2)

    const axesHelper = new THREE.AxesHelper( 50 );
    scene.add( axesHelper)
    return scene
}

/**
 * @description 创建相机
 * @returns {PerspectiveCamera}
 * @constructor
 */

export function UseCreateCamera(dom){
    const camera = new THREE.PerspectiveCamera(
        75,
        dom.clientWidth / dom.clientHeight,
        0.1,
        1000
    );
    camera.lookAt(0,0,0)
    camera.position.set(0, 0, 100);
    return camera
}


let globalRenderCanvas = null;
export function UseCreateRenderer(dom){
    const renderer = new THREE.WebGLRenderer({
        antialias: true,
        autoClear: true,
        alpha: true,
    });
    renderer.setSize(dom.clientWidth, dom.clientHeight);
    globalRenderCanvas = renderer.domElement;
    dom.appendChild(globalRenderCanvas);
    return renderer
}


/**
 * @description 创建灯光
 * @constructor
 */
export function UseCreateLight(type){

    switch (type){
        case 'spotLight':
            let  spotLight = new THREE.SpotLight(0xffffff, 1,0, Math.PI/2 );
            spotLight.position.set(0, 0, 100);
            spotLight.castShadow = true; // 让光源产生阴影
            spotLight.shadowMapWidth = spotLight.shadowMapHeight = 1024*4;
            spotLight.shadow.mapSize.width = 1024*4
            spotLight.shadow.mapSize.height = 1024*4

            spotLight.shadow.camera.near = 50;
            spotLight.shadow.camera.far = 4000;
            spotLight.shadow.camera.fov = 50
            return spotLight;

        case 'ambientLight':
            let ambientLight = new THREE.AmbientLight(0xffffff, 1);
            return ambientLight;

    }
}



/**
 * @description 创建控制器
 * @constructor
 */
export function UseCreateControls(camera, renderer){
// 设置相机控件轨道控制器OrbitControls
    const controls = new OrbitControls(camera, renderer.domElement);
    return controls;
}

/**
 *
 * @constructor
 */
export function UseCreateGUIHelp(){
    const gui = new GUI()
    const guiParams = {
        topColor: "#b4eeea",
        sideColor: "#123024",
        scale: 0.1,
    }
    var params = new function () {
        this.color = 0x00ff00; //颜色
        this.size = 1; //粒子大小
    };
    gui.addColor(params, "color").onChange(e => { //点击颜色面板，e为返回的10进制颜色
        console.log("color changed. ", e);
    });
    // 设置 GUI 控件的 Z 轴层级为较大的值，确保其位于其他元素之上
    gui.domElement.style.zIndex = "9999";

    // 设置 GUI 控件的位置
    gui.domElement.style.position = "absolute";
    gui.domElement.style.right = "0%"; // 将 GUI 控件水平居中
    gui.domElement.style.top = "5%"; // 将 GUI 控件垂直居中
    // gui.domElement.style.transform = "translate(-50%, -50%)"; // 通过 transform 属性进行居中对齐
}

export function UseCreateRaycaster($event,camera,meshGroup,dom,onClickCallback){

    let raycaster = new THREE.Raycaster();
    const px = event.offsetX;
    const py = event.offsetY;
    // 屏幕坐标转为标准设备坐标
    const x = (px / dom.clientWidth) * 2 - 1;
    const y = -(py / dom.clientHeight) * 2 + 1;
    //创建一个射线投射器`Raycaster`
    //通过鼠标单击位置标准设备坐标和相机参数计算射线投射器`Raycaster`的射线属性.ray
    raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
    let intersects = raycaster.intersectObjects(meshGroup);
    onClickCallback(intersects);
    return raycaster;
}


export function UseCreateSprite(){

}

/**
 * @description 创建飞线
 * @param start
 * @param end
 * @constructor
 */
export function UseCreateFlyLine(start,end){

}

export function UseCreateMarker(){

}